using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

/// <summary>
/// 挂在 canvas 上，控制人物状态栏、快捷道具栏、背包栏的 UI
/// </summary>
public class InventoryManager : Singleton<InventoryManager>
{
    /// <summary>
    /// 拖拽前，保存物品的数据
    /// </summary>
    public class DragData
    {
        // 拖拽前的物品格子
        public SlotUI originSlotUI;
        // 格子的父物体
        public RectTransform originParent;
    }

    [Header("Inventory Data")]
    public InventoryData_SO inventoryDataTemplate;
    public InventoryData_SO inventoryData;
    public InventoryData_SO actionDataTemplate;
    public InventoryData_SO actionData;
    public InventoryData_SO equipmentDataTemplate;
    public InventoryData_SO equipmentData;
    [Header("Contains")]
    public ContainerUI inventoryUI;
    public ContainerUI actionUI;
    public ContainerUI equipmentUI;
    [Header("Drag Canvas")]
    public Canvas dragCanvas;
    public DragData currentDrag;
    [Header("UI Panel")]
    public GameObject bagPanel;
    public GameObject statPanel;

    bool isOpen;
    [Header("Stat Text")]
    public Text hpText;
    public Text atkText;
    [Header("ToolTip")]
    public ItemToolTip toolTip;

    protected override void Awake()
    {
        base.Awake();
        // 使用 DontDestroyOnLoad 必须是在场景中的根物体上
        DontDestroyOnLoad(gameObject);
        if (inventoryDataTemplate != null)
        {
            inventoryData = Instantiate(inventoryDataTemplate);
        }
        if (actionDataTemplate != null)
        {
            actionData = Instantiate(actionDataTemplate);
        }
        if (equipmentDataTemplate != null)
        {
            equipmentData = Instantiate(equipmentDataTemplate);
        }
    }
    private void Start()
    {
        LoadData();

        inventoryUI.RefreshUI();
        actionUI.RefreshUI();
        equipmentUI.RefreshUI();
        // 继续游戏时,重新装备武器
        CheckEquipmentInPlayer();
    }
    public void CheckEquipmentInPlayer()
    {
        var equip = equipmentData.inventoryList[0];
        {
            if (equip != null && equip.itemData != null && equip.itemData.itemType == ItemType.WEAPON)
            {
                // 装备武器
                // GameManager.Instance.playerStat.EquipWeapon(equip.itemData);
                equipmentUI.slotUIs[0].UpdateItemUI();
            }
        }
    }
    private void Update()
    {
        if (Input.GetKeyDown(KeyCode.B))
        {
            // 打开时更新属性面板
            InventoryManager.Instance.UpdateHPAndAtk(GameManager.Instance.playerStat.MaxHealth, GameManager.Instance.playerStat.attackData.minDamage, GameManager.Instance.playerStat.attackData.maxDamage);

            isOpen = !isOpen;
            bagPanel.SetActive(isOpen);
            statPanel.SetActive(isOpen);
        }
    }
    public void SaveData()
    {
        SaveManager.Instance.Save(inventoryData, inventoryData.name);
        SaveManager.Instance.Save(actionData, actionData.name);
        SaveManager.Instance.Save(equipmentData, equipmentData.name);
    }
    public void LoadData()
    {
        SaveManager.Instance.Load(inventoryData, inventoryData.name);
        SaveManager.Instance.Load(actionData, actionData.name);
        SaveManager.Instance.Load(equipmentData, equipmentData.name);
    }
    /// <summary>
    /// 更新血量和攻击显示
    /// </summary>
    /// <param name="hp"></param>
    /// <param name="minAtk"></param>
    /// <param name="maxAtk"></param>
    public void UpdateHPAndAtk(int hp, int minAtk, int maxAtk)
    {
        hpText.text = hp.ToString();
        atkText.text = minAtk.ToString() + " - " + maxAtk.ToString();
    }
    #region 检查是否拖拽物品到 背包、快捷道具栏、装备栏等 的某一个格子 slotUI 上
    public bool CheckInInventoryUI(Vector3 pos)
    {
        for (int i = 0; i < inventoryUI.slotUIs.Length; i++)
        {
            RectTransform t = inventoryUI.slotUIs[i].transform as RectTransform;

            // 判断传入的鼠标位置的点是否在 UI 矩形范围内
            if (RectTransformUtility.RectangleContainsScreenPoint(t, pos))
            {
                return true;
            }
        }
        return false;
    }
    public bool CheckInActionUI(Vector3 pos)
    {
        for (int i = 0; i < actionUI.slotUIs.Length; i++)
        {
            RectTransform t = actionUI.slotUIs[i].transform as RectTransform;

            // 判断传入的鼠标位置的点是否在 UI 矩形范围内
            if (RectTransformUtility.RectangleContainsScreenPoint(t, pos))
            {
                return true;
            }
        }
        return false;
    }
    public bool CheckInEquipmentUI(Vector3 pos)
    {
        for (int i = 0; i < equipmentUI.slotUIs.Length; i++)
        {
            RectTransform t = equipmentUI.slotUIs[i].transform as RectTransform;

            // 判断传入的鼠标位置的点是否在 UI 矩形范围内
            if (RectTransformUtility.RectangleContainsScreenPoint(t, pos))
            {
                return true;
            }
        }
        return false;
    }
    #endregion
    #region 检查任务完成情况：在接收任务后立即检查背包中的物品是否满足收集类任务的要求
    /// <summary>
    /// 检查背包中的物品是否满足任务需求，更新任务完成进度
    /// </summary>
    /// <param name="questItemName"></param>
    public void CheckQuestItemInbag(string questItemName)
    {
        // 检查背包中所有物品
        foreach (var inventoryItem in inventoryData.inventoryList)
        {
            // 不为空的物品
            if (inventoryItem.itemData != null)
            {
                // 物品名字和任务需求的名字相同
                if (inventoryItem.itemData.itemName == questItemName)
                {
                    // 更新任务需求完成进度
                    QuestManager.Instance.UpdateQuestProcess(inventoryItem.itemData.itemName, inventoryItem.inventoryAmount);
                }
            }
        }

        foreach (var inventoryItem in actionData.inventoryList)
        {
            // 不为空的物品
            if (inventoryItem.itemData != null)
            {
                // 物品名字和任务需求的名字相同
                if (inventoryItem.itemData.itemName == questItemName)
                {
                    // 更新任务需求完成进度
                    QuestManager.Instance.UpdateQuestProcess(inventoryItem.itemData.itemName, inventoryItem.inventoryAmount);
                }
            }
        }
    }
    #endregion
    #region 检查背包和快捷栏的物品
    /// <summary>
    /// 检查背包中物品是否存在
    /// </summary>
    /// <param name="item"></param>
    /// <returns>存在返回该物品，否则返回空</returns>
    public InventoryItem QuestItemInBag(ItemData_SO item)
    {
        return inventoryData.inventoryList.Find(i => i.itemData == item);
    }
    public InventoryItem QuestItemInAction(ItemData_SO item)
    {
        return actionData.inventoryList.Find(i => i.itemData == item);
    }
    public InventoryItem QuestItemInEquipment(ItemData_SO item)
    {
        return equipmentData.inventoryList.Find(i => i.itemData == item);
    }
    #endregion
}
